package com.backend.demo.tag;

import javax.servlet.jsp.JspWriter;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.backend.demo.dao.EnumItemDao;
import com.backend.demo.entities.EnumItem;
import com.backend.demo.util.StringUtils;

/**
 * 枚举自定义标签类
 * @author Administrator
 *
 */
public class EnumItemTag extends RequestContextAwareTag {

	private static final long serialVersionUID = 1L;
	
	private EnumItemDao enumItemDao;
	
	private String enumword;
	
	private String code;
	
	private String cssClass;
	
	private String cssId;
	
	private String attrName;
	
	private String language;
	
	public String getEnumword() {
		return enumword;
	}

	public void setEnumword(String enumword) {
		this.enumword = enumword;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getCssId() {
		return cssId;
	}

	public void setCssId(String cssId) {
		this.cssId = cssId;
	}

	public String getAttrName() {
		return attrName;
	}

	public void setAttrName(String attrName) {
		this.attrName = attrName;
	}
	
	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	@Override
	protected int doStartTagInternal() throws Exception {
		enumItemDao = (EnumItemDao) this.getRequestContext().getWebApplicationContext().getBean("enumItemDao");
		JspWriter out = pageContext.getOut();
		EnumItem enumItem = enumItemDao.getNameByEnumwordAndCode(enumword, code);
		if(StringUtils.isNotEmpty(attrName)){
			StringBuffer stringBuffer = new StringBuffer();
			if("zh_CN".equals(language)){
				stringBuffer.append("<input type='text' disabled='disabled' name='"+attrName+"' value='"+enumItem.getName()+"'");
			}else{
				stringBuffer.append("<input type='text' disabled='disabled' name='"+attrName+"' value='"+enumItem.getNameen()+"'");
			}
			if (StringUtils.isNotEmpty(cssClass)) {
				stringBuffer.append("class='" + cssClass+"'");
			}
			if (StringUtils.isNotEmpty(cssId)) {
				stringBuffer.append("id='" + cssId+"'");
			}
			stringBuffer.append(">");
			out.print(stringBuffer.toString());
		}else{
			if("zh_CN".equals(language)){
				out.print(enumItem.getName());
			}else{
				out.print(enumItem.getNameen());
			}
		}
		
		return 0;
	}

}
